package org.gap.wizards.world;

import java.awt.Point;

import org.gap.wizards.InGameState;
import org.gap.wizards.utils.Defect;


public enum Movement {
	UP(InGameState.UP), 
	DOWN(InGameState.DOWN), 
	LEFT(InGameState.LEFT), 
	RIGHT(InGameState.RIGHT), 
	UP_LEFT(InGameState.UP_LEFT), 
	UP_RIGHT(InGameState.UP_RIGHT), 
	DOWN_RIGHT(InGameState.DOWN_RIGHT), 
	DOWN_LEFT(InGameState.DOWN_LEFT), 
	TRANSITION(InGameState.TRANSITION);
	
	char keyBinding;
	Movement(char key) {
		keyBinding = key;
	}
	
	public static boolean isValid(char keyPress) {
		return lookup(keyPress) != null;
	}
	
	public static Movement from(char keyPress) {
		Movement found = lookup(keyPress);
		if (found == null) {
			throw new Defect("Only certain keys are defined for movement, [" + keyPress + "] is not one of them.");
		}
		return found;
	}
	
	private static Movement lookup(char keyPress) {
		for (Movement each : Movement.values()) {
			if (each.keyBinding == keyPress) {
				return each;
			}
		}
		return null;
	}

	public Point adjustLocation(Point pcLocation) {
		switch (this) {
		case UP:
			return new Point(pcLocation.x, pcLocation.y - 1);
		case DOWN:
			return new Point(pcLocation.x, pcLocation.y + 1);
		case LEFT:
			return new Point(pcLocation.x - 1, pcLocation.y);
		case RIGHT:
			return new Point(pcLocation.x + 1, pcLocation.y);
		case UP_LEFT:
			return new Point(pcLocation.x - 1, pcLocation.y - 1);
		case DOWN_RIGHT:
			return new Point(pcLocation.x + 1, pcLocation.y + 1);
		case DOWN_LEFT:
			return new Point(pcLocation.x - 1, pcLocation.y + 1);
		case UP_RIGHT:
			return new Point(pcLocation.x + 1, pcLocation.y - 1);
		};
		return pcLocation;
	}
}
